from apps.system.role.baseViews import *

class RoleViewSet(BaseRoleViewSet):
    
    def get_queryset(self):
        params = getRequestParams(self.request)
        _lists =RoleDao().findAll().prefetch_related('routers')
        if self.request.user.role.key == 'superAdmin':
            _lists = _lists
        elif self.request.user.role.key == 'admin':
            _lists = _lists.exclude(key='superAdmin')
        
        return _lists
    
    @action(methods=['post'], detail=False)
    def updateRole(self, request):
        params = getRequestParams(self.request)
        obj_id = params.get('roleId')
        if isEmptyStr(obj_id):
            return Response(response_message('roleId必填'))
        role = RoleDao().findOneByObjId(obj_id)
        print('role', role)
        if params.get('routerIds') and len(params.get('routerIds'))>0:
            ids = role.routers.all().values_list('id', flat=True)
            print('ids', ids)
            role.routers.remove(*ids)
            role.routers.add(*params.get('routerIds'))
        RoleDao().update(obj_id, params)
        return Response(response_success('修改成功'))